package com.xps.and.yuntong.Ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.xps.and.yuntong.Data.net.UserNetWorks;
import com.xps.and.yuntong.R;
import com.xps.and.yuntong.Utils.AppCommond;
import com.xps.and.yuntong.Utils.CommonUtils;
import com.xps.and.yuntong.Utils.DownTimer;
import com.xps.and.yuntong.base.BaseActivity;
import com.jude.utils.JUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/7/31.
 */

public class UpDPasswordActivity extends BaseActivity {
    public static int Interval = 1000;
    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.et_phoneNum)
    EditText etPhoneNum;
    @BindView(R.id.et_verifyCode)
    EditText etVerifyCode;
    @BindView(R.id.Verification_TextView)
    TextView VerificationTextView;
    @BindView(R.id.XN_phoneNum)
    EditText XNPhoneNum;
    @BindView(R.id.XM_phoneNum)
    EditText XMPhoneNum;
    @BindView(R.id.NextStep_Button)
    Button NextStepButton;
    DownTimer timer;
    @BindView(R.id.actionbar_tv_title)
    TextView actionbarTvTitle;

    @Override
    protected int getLayout() {
        return R.layout.activity_uppassword;
    }

    @Override
    protected void setData() {

    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        actionbarTvTitle.setText("修改密码");
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
    }

    @OnClick({R.id.actionbar_iv_back, R.id.Verification_TextView, R.id.NextStep_Button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                finish();
                break;
            case R.id.Verification_TextView:
                if (!CommonUtils.isMobileNumber(etPhoneNum.getText
                        ().toString().trim())) {
                    JUtils.Toast("手机号码格式不合法");
                    return;
                }else{
//                    Intent intent = new Intent(UpDPasswordActivity.this, TranslateActivity.class);
//                    intent.putExtra("etphoneNum", etPhoneNum.getText().toString());
//                    intent.putExtra("leibie", "1");
//                    intent.putExtra("activity","UpDPasswordActivity");
//                    startActivity(intent);
                }

                break;
            case R.id.NextStep_Button:
                String XNPhoneNumStr = XNPhoneNum.getText().toString();
                String XMPhoneNumStr = XMPhoneNum.getText().toString();
                if (XNPhoneNumStr.equals(XMPhoneNumStr)) {
                } else {
                    JUtils.Toast("两次密码不同");
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(AppCommond a) {
        if (!AppCommond.check(a)) return;
        for (String activityName : a.getAcNameList()) {
            if (!activityName.equals(getClass().getSimpleName())) {
                VerificationTextView.setEnabled(false);
                timer = new DownTimer();
                timer.setTotalTime(60 * Interval);
                timer.setIntervalTime(Interval);
                timer.setTimerLiener(new DownTimer.TimeListener() {
                    @Override
                    public void onFinish() {
                        VerificationTextView.setEnabled(true);
                        VerificationTextView.setText("获取验证码");
                    }

                    @Override
                    public void onInterval(long remainTime) {
                        VerificationTextView.setText(remainTime / 1000
                                + "秒后" + "再试");
                    }
                });
                timer.start();
            }
        }
    }
}